﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssertException.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the AssertException type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Testing
{
    using System;
    using System.Diagnostics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    public static class AssertException
    {
        [DebuggerStepThrough]
        public static T Throws<T>(Action action) where T : Exception
        {
            Exception e = null;
            try
            {
                action();
            }
            catch (Exception ex)
            {
                e = ex;
            }

            if (e == null)
            {
                Assert.Fail("Expected exception type: {0} but no exception was thrown.", typeof(T).Name);
            }

            Assert.AreEqual(typeof(T), e.GetType(), "Expected exception of type: {0:s}, actual: {1:s}.", typeof(T).Name, e.GetType().Name);
            return (T)e;
        }

        [DebuggerStepThrough]
        public static T Throws<T>(Action action, string expectedMessage) where T : Exception
        {
            var result = Throws<T>(action);
            Assert.AreEqual(expectedMessage, result.Message);
            return result;
        }
    }
}